<?php

/**
 * @filename source_module.php 2013-11-11 02:10:56x
 * @author Huming Xu <huming17@126.com>
 * @version 1.0.0
 * @copyright DZF (c) 2013, Huming Xu
 */
class ctrl_user_main extends user_main {

    public $info_array = array();
    public $page_array = array();
    public $tree_array = array();

    static function &instance() {
        static $object;
        if (empty($object)) {
            $object = new self();
        }
        return $object;
    }

    public function do_mongotest(){
        $pagestartime = microtime();
//        
//        for ($i=0; $i<1000; $i++) {
//            $m = new Mongo("mongodb://120.27.248.173:27017/sadmin:123456");
//        }
//        
        $m = new CMongoDb();
        
        $res = $m->getAll('User');//一次性获取整张表的数据
//        $res = $m->getAll('User', ['key' => 't_key_5']);//带查询条件一次性获取所有数据
//        $res = $m->getOne('User', ['key' => 't_key_6']);//带查询条件获取唯一一条数据 如果没有该数据的话返回NULL
//        $cnt = $m->getCount('User', ['key' => 't_key_5']);//获取指定条件的数量
//        $r = $m->toDelete('User', ['key' => 't_key_5']);//删除指定key值的数据
        
//        $salt = time();
        
//        $r = $m->toInsert('User', ['key' => 't_key_'.$salt, 'val' => '11111', 'ctime' => $salt]);//新增操作
//        $r = $m->toUpdate('User', ['key' => 't_key_5'], ['val' => '22222', 'ctime' => time()]);//更新操作

//        $r = $m->getAll('User', ['ctime' => ['$gt' => 1494831984]]);//大于
//        $r = $m->getAll('User', ['ctime' => ['$gte' => 1494831984]]);//大于等于
//        $r = $m->getAll('User', ['ctime' => ['$lt' => 1494831984]]);//小余等于
//        $r = $m->getAll('User', ['ctime' => ['$lte' => 1494831984]]);//小余等于
//        $r = $m->getAll('User', ['ctime' => ['$gte' => 1494831984, '$lte' => time()]]);//大于1494831984 小余当前
//        $r = $m->getAll('User', ['ctime' => ['$ne' => 1494831984]]);//不等于
        
//        $r = $m->getAll('User', ['key' => ['$in' => ['t_key_2', 't_key_3']]]);//in 操作
//        $r = $m->getAll('User', ['key' => ['$nin' => ['t_key_2', 't_key_3']]]);//not in 操作
        
//        $r = $m->getAll('User', ['key' => new MongoRegex('/.*t_key.*/i')]);//like 操作
        
        
        P($res);
        
        $pageendtime = microtime(); 
        $starttime = explode(" ",$pagestartime); 
        $endtime = explode(" ",$pageendtime); 
        $totaltime = $endtime[0]-$starttime[0]+$endtime[1]-$starttime[1]; 
        $timecost = sprintf("%s",$totaltime); 
        echo "页面运行时间: $timecost 秒"; 
    }
    
    /*
     * 	取出一条信息
     */

    public function do_detail($user_id = '') {
        $detail = array();
        $user_id = isset($_REQUEST['user_id']) ? $_REQUEST['user_id'] : '';
        $detail = $this->one_info('user', 'user_id', $user_id);
        //DEBUG 调试输出
        include template('user/main/detail');
    }

    /*
     *  校验用户名是否已存在
     * $out_put 是否输出 1 总是输出并结束 2 仅失败输出
     */

    public function do_check_user_name($out_put = 1) {
        global $_G;
        $return = array('code' => '1');
        $user_main = isset($_REQUEST['user_main']) && !empty($_REQUEST['user_main']) ? $_REQUEST['user_main'] : '';
        if (!empty($user_main['user_name'])) {
            $check_user = $this->check_user_exist($user_main['user_name']);
            if (empty($check_user)) {
                $return['code'] = '0';
            } else {
                $return['code'] = '1';
            }
        } else {
            $return['code'] = '2';
        }
        if ($out_put == 1 || ($out_put == 2 && $return['code'] != 0)) {
            if ($_G['gp_api']) {
                echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
            } else {
                echo format_data($return, 'json', $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
                if ($out_put == 2 && $return['code'] != 0) {
                    die();
                }
            }
        }
    }

    public static function check_user_password($user_id, $user_password = 0) {
        global $_G;
        $check_user_password = $wheresql = '';
        if ($user_password && $user_id) {
            $wheresql = " AND user_password = '" . encode_password($user_password) . "'";
        }
        if (!empty($wheresql)) {
            $check_user_password = DB::result_first('SELECT user_id FROM ' . DB::table('user') . " WHERE user_id='" . $user_id . "' " . $wheresql . " LIMIT 1");
        }
        return $check_user_password;
    }

    /*
     *  提交用户注册
     */

    public function do_add() {
        global $_G;
        /*
          $user_name = isset($_REQUEST['user_name']) && !empty($_REQUEST['user_name']) ? $_REQUEST['user_name']:'';
          $user_password = isset($_REQUEST['user_password']) && !empty($_REQUEST['user_password']) ? $_REQUEST['user_password']:'';
          $user_phone = isset($_REQUEST['user_phone']) && !empty($_REQUEST['user_phone']) ? $_REQUEST['user_phone']:'';
          $user_nickname = isset($_REQUEST['user_nickname']) && !empty($_REQUEST['user_nickname']) ? $_REQUEST['user_nickname']:'';
          $in_school = isset($_REQUEST['in_school']) && !empty($_REQUEST['in_school']) ? $_REQUEST['in_school']:'';//1 表示不在校 2表示在校
          $am_id_1 = isset($_REQUEST['am_id_1']) && !empty($_REQUEST['am_id_1']) ? $_REQUEST['am_id_1']:'';
          $am_id_2 = isset($_REQUEST['am_id_2']) && !empty($_REQUEST['am_id_2']) ? $_REQUEST['am_id_2']:'';
          $am_id_3 = isset($_REQUEST['am_id_3']) && !empty($_REQUEST['am_id_3']) ? $_REQUEST['am_id_3']:'';
          $sm_id = isset($_REQUEST['sm_id']) && !empty($_REQUEST['sm_id']) ? $_REQUEST['sm_id']:'';
          $grade_num = isset($_REQUEST['grade_num']) && !empty($_REQUEST['grade_num']) ? $_REQUEST['grade_num']:'';
          $class_num = isset($_REQUEST['class_num']) && !empty($_REQUEST['class_num']) ? $_REQUEST['class_num']:'';
         */
        //DEBUG 校验短信是否通过
        $sms_main_check = auto_mad('sms', 'main', 'check');
        $sms_main_check_array = json_decode($sms_main_check, true);
        if ($sms_main_check_array['code'] != '0') {
            echo $sms_main_check;
            die;
        }
        //DEBUG 
        $user_main = isset($_REQUEST['user_main']) && !empty($_REQUEST['user_main']) ? $_REQUEST['user_main'] : '';
        $user_detail = isset($_REQUEST['user_detail']) && !empty($_REQUEST['user_detail']) ? $_REQUEST['user_detail'] : '';
        //DEBUG 校验非空字段 用户 密码 昵称不能为空
        if (!empty($user_main['user_name']) && !empty($user_main['user_password'])) {
            if(empty($user_detail['user_nickname'])){
                $user_detail['user_nickname']=$user_main['user_name'];
            }
            //DEBUG 校验用户是否已存在
            $check_user = $this->check_user_exist($user_main['user_name']);
            //DEBUG 校验手机号码是否已存在
            $check_user_id = $this->check_user_exist($sms_main_check_array['data']['sms_main']['sms_phone']);
            if (empty($check_user) && empty($check_user_id)) {
                $user_password_encode = encode_password($user_main['user_password']);
                //获取接收短信的手机号码
                $user_main_data = array(
                    "user_name" => $user_main['user_name'],
                    "user_password" => $user_password_encode,
                    "ischeck" => 2,
                    "create_dateline" => TIMESTAMP,
                    "modify_dateline" => TIMESTAMP
                );
                $user_detail_data = $user_detail;
                $user_detail_data['user_phone'] = $sms_main_check_array['data']['sms_main']['sms_phone'];
                $user_detail_data['create_dateline'] = TIMESTAMP;
                $user_detail_data['modify_dateline'] = TIMESTAMP;
                $user_main_insert_id = DB::insert('user', $user_main_data, true);
                if ($user_main_insert_id) {
                    $user_detail_data['user_id'] = $user_main_insert_id;
                    $user_detail_insert_id = DB::insert('user_detail', $user_detail_data, true);
                    $return['code'] = '0';
                } else {
                    $return['code'] = '3';
                }
            } else {
                $return['code'] = '1';
            }
        } else {
            //DEBUG TODO 跳转登出
            $return['code'] = '4';
        }
        if ($_G['gp_api']) {
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }
    }

    /*
     *  用户密码重置
     */

    public function do_reset_user_password() {
        global $_G;
        /*
          $user_name = isset($_REQUEST['user_name']) && !empty($_REQUEST['user_name']) ? $_REQUEST['user_name']:'';
          $user_password = isset($_REQUEST['user_password']) && !empty($_REQUEST['user_password']) ? $_REQUEST['user_password']:'';
          $user_phone = isset($_REQUEST['user_phone']) && !empty($_REQUEST['user_phone']) ? $_REQUEST['user_phone']:'';
          $user_nickname = isset($_REQUEST['user_nickname']) && !empty($_REQUEST['user_nickname']) ? $_REQUEST['user_nickname']:'';
          $in_school = isset($_REQUEST['in_school']) && !empty($_REQUEST['in_school']) ? $_REQUEST['in_school']:'';//1 表示不在校 2表示在校
          $am_id_1 = isset($_REQUEST['am_id_1']) && !empty($_REQUEST['am_id_1']) ? $_REQUEST['am_id_1']:'';
          $am_id_2 = isset($_REQUEST['am_id_2']) && !empty($_REQUEST['am_id_2']) ? $_REQUEST['am_id_2']:'';
          $am_id_3 = isset($_REQUEST['am_id_3']) && !empty($_REQUEST['am_id_3']) ? $_REQUEST['am_id_3']:'';
          $sm_id = isset($_REQUEST['sm_id']) && !empty($_REQUEST['sm_id']) ? $_REQUEST['sm_id']:'';
          $grade_num = isset($_REQUEST['grade_num']) && !empty($_REQUEST['grade_num']) ? $_REQUEST['grade_num']:'';
          $class_num = isset($_REQUEST['class_num']) && !empty($_REQUEST['class_num']) ? $_REQUEST['class_num']:'';
         */
        //DEBUG 校验短信是否通过
        $sms_main_check = auto_mad('sms', 'main', 'check');
        $sms_main_check_array = json_decode($sms_main_check, true);
        if ($sms_main_check_array['code'] != '0') {
            echo $sms_main_check;
            die;
        }
        //DEBUG 
        $user_main = isset($_REQUEST['user_main']) && !empty($_REQUEST['user_main']) ? $_REQUEST['user_main'] : '';
        if (!empty($user_main['user_phone']) && !empty($user_main['user_password'])) {
            //DEBUG 校验用户是否已存在
            $check_user_id = $this->check_user_exist($sms_main_check_array['data']['sms_main']['sms_phone']);
            if (!empty($check_user_id)) {
                $user_password_encode = encode_password($user_main['user_password']);
                $user_main_data = array(
                    "user_password" => $user_password_encode
                );
                $reset_user_password_result = DB::update('user', $user_main_data, array('user_id' => $check_user_id));
                $return['code'] = '0';
            } else {
                $return['code'] = '1'; //用户名不存在
            }
        } else {
            //DEBUG TODO 跳转登出
            $return['code'] = '1';
        }
        if ($_G['gp_api']) {
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }
    }

    /*
     *  修改我的帐号密码
     */

    public function do_reset_my_password() {
        global $_G;
        //DEBUG 
        $user_id = isset($_REQUEST['user_id']) && !empty($_REQUEST['user_id']) ? $_REQUEST['user_id'] : $_G['user_id'];
        $user_main = isset($_REQUEST['user_main']) && !empty($_REQUEST['user_main']) ? $_REQUEST['user_main'] : '';
        if (!empty($user_main['user_password_old']) && !empty($user_main['user_password_new'])) {
            //DEBUG 校验用户旧密码是否正确
            $check_user_id = $this->check_user_password($user_id, $user_main['user_password_old']);
            if (!empty($check_user_id)) {
                $data = array('user_password' => encode_password($user_main['user_password_new']));
                $reset_user_password_result = DB::update('user', $data, array('user_id' => $check_user_id));
                $return['code'] = '0';
            } else {
                $return['code'] = '1'; //旧密码错误
            }
        } else {
            //DEBUG TODO 跳转登出
            $return['code'] = '1';
        }
        if ($_G['gp_api']) {
            echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        }
    }

    /*
     *  编辑系统信息
     */

    public function do_edit() {
        global $_G;
    }

    /*
     *  删除系统信息
     */

    public function do_delete() {
        global $_G;
    }

    /*
     *  系统信息列表
     */

    public function do_index() {
        global $_G;
    }

    /*
     *  登录信息
     */

    public function do_login() {
        global $_G;
        
        $user_name = isset($_REQUEST['user_name']) ? $_REQUEST['user_name'] : '';
        $user_password = isset($_REQUEST['user_password']) ? $_REQUEST['user_password'] : '';
        
        $return['code'] = '5';
        $return['data'] = [];
        
        if($user_name && $user_password){
            $userInfo = $this->one_info('user', 'user_name', $user_name);
            
            if(!empty($userInfo) && is_array($userInfo)){//存在这个用户
                //校验一下密码
                $user_encode_password = $userInfo['user_password'];
                if($user_encode_password == encode_password($user_password)){//走到这里说明账号密码都是正确的
                    $user_id = $userInfo['user_id'];
                    //开始生成token
                    $randTime = TIMESTAMP;
                    $token = ext::authcode("$user_encode_password\t$user_id\t$randTime", 'ENCODE');
                    //插入token表数据
                    insertToken($token, $user_id);
                    //插入用户登录日志表数据
                    insertLoginLog($token, $user_id);
                    
                    $return['code'] = '0';
                    $return['data']['user']['token'] = $token;
                    $return['data']['user']['user_id'] = $userInfo['user_id'];
                    $return['data']['user']['user_name'] = $userInfo['user_name'];
                    $return['data']['user']['ischeck'] = $userInfo['ischeck'];
                    //获取用户角色
                    $roleRes = $this->one_info('user_role_relation', 'user_id', $user_id);
                    $roleId = $return['data']['user']['role_id'] = isset($roleRes['role_id']) ? $roleRes['role_id'] : 0;//用户的角色ID
                    //根据角色id获取角色名称
                    $rInfo = $this->one_info('user_role', 'role_id', $roleId);
                    $return['data']['user']['role_name'] = isset($rInfo['role_name']) ? $rInfo['role_name'] : '';//用户的角色名称
                    //DEBUG 接口增加 头像接口
                    $detail = $this->one_info('user_detail', 'user_id', $user_id);
                    if(!empty($detail) && is_array($detail)){//说明有结果集
                        $sm_id = $detail['sm_id'];//学校id，对应换成网校id so_id
                        //pre_sm_so
                        $sInfo = $this->one_info('sm_so', 'sm_id', $sm_id);
                        $return['data']['user']['so_id'] = isset($sInfo['so_id']) ? $sInfo['so_id'] : 0;//用户的网校id
                    }
                    
                    $return['data']['user']['user_avatar'] = $detail['user_avatar'] ? $detail['user_avatar'] : 'http://fstore.jxzhjy.com/data/upload/user_avatar.png';
                    
                    $return['data']['user']['app_token'] = '';//题库对应的app_token
                }
            }
        }
        
        echo format_data($return, $_G['gp_api'], $_G['gp_mod'], $_G['gp_action'], $_G['gp_do']);
        return false;
    }

}

?>